﻿<template>
	<!-- 页面主体 -->
	<view>
		<!--提示框组件-->
		<u-toast ref="uToast" />
		<!--无网络组件-->
		<u-no-network></u-no-network>
		<!--头部组件-->
		<!-- <u-navbar leftIcon="search" :title="appTitle" safeAreaInsetTop fixed placeholder @leftClick="goSearch"></u-navbar> -->
		<u-navbar leftIcon=" " :title="appTitle" safeAreaInsetTop fixed placeholder></u-navbar>
		<!-- <u-button @click="onclick" text="回调"></u-button> -->
		<!--首页模块化组合组件-->
		<coreshop-page :coreshopdata="pageData"></coreshop-page>
		<!--版权组件-->
		<coreshop-copyright v-if="copy"></coreshop-copyright>
		<!--客服组件-->
		<!-- #ifdef APP-PLUS-NVUE || APP-PLUS -->
		<!-- <view class="floatingButton" @click="showChat()">
			<u-icon name="server-man" color="#e54d42" size="45"></u-icon>
		</view> -->
		<!-- #endif -->
		<!-- #ifdef MP-WEIXIN -->
		<!-- <button class="floatingButton" hover-class="none" open-type="contact" bindcontact="showChat" :session-from="kefupara">
			<u-icon name="server-man" color="#e54d42" size="45"></u-icon>
		</button> -->
		<!-- #endif -->
		<!--返回顶部组件-->
		<u-back-top :scroll-top="scrollTop" :duration="500"></u-back-top>
		<!--弹出框-->
		<coreshop-modal-img :show="modalShow" :src="indexPopupWindowImageUrl" @imgTap="imgTap" @closeTap="closeTap" />
		<!-- 登录提示 -->
		<coreshop-login-modal @changeEvent="initData()"></coreshop-login-modal>
	</view>
</template>
<script>
	import {
		mapState
	} from 'vuex';
	export default {
		data() {
			return {
				currentIndex: 0,
				opacity: 0,
				scrollTop: 0,
				imageUrl: '/static/images/ShareImage.png', //店铺分享图片
				pageData: [],
				pageCode: 'mobile_home', //页面布局编码
				kefupara: '', //客服传递资料
				copy: false,
				shareUrl: this.$globalConstVars.shareUrl,
				isScorll: false,
				homeTitle: '',
				modalShow: false,
				indexPopupWindowHrefUrl: '',
				indexPopupWindowImageUrl: '',
				cardData: {
					id: 7001,
					pageCode: "mobile_home",
					widgetCode: "cards",
					parameters:{
						list:[]
					}
				},
				projectData: {
					id: 7002,
					pageCode: "mobile_home",
					widgetCode: "project",
					parameters:{
						list:[]
					}
				},
			};
		},
		// updated() {
		// 	this.copy = true;
		// },
		mounted() {
			this.modalShow = this.$store.state.config.showIndexPopupWindow == 1;
			this.indexPopupWindowHrefUrl = this.$store.state.config.indexPopupWindowHrefUrl;
			this.indexPopupWindowImageUrl = this.$store.state.config.indexPopupWindowImageUrl;
		},
		computed: {
			// ...mapState({
			// 	hasLogin: state => state.hasLogin,
			// 	userInfo: state => state.userInfo,
			// 	userStore: state => state.userStore,
			// }),
			...mapState(['hasLogin','userInfo', 'userStore']),
			hasLogin: {
				get() {
					return this.$store.state.hasLogin;
				},
				set(val) {
					this.$store.commit('hasLogin', val);
				}
			},
			userInfo: {
				get() {
					return this.$store.state.userInfo;
				},
				set(val) {
					this.$store.commit('userInfo', val);
				}
			},
			appTitle() {
				this.homeTitle = this.$store.state.config.shopName;
				return this.homeTitle || '';
			},
			// 获取店铺联系人手机号
			shopMobile() {
				return this.$store.state.config.shopMobile || 0;
			}
		},
		onShow(e) {
			this.getCaeds()
			this.getProjects()
			this.initData();
		},
		methods: {
			onclick(){
				this.$u.api.paywca().then(res=>{
					console.log('s', res)
				})
			},
			imgTap() {
				this.modalShow = false;
				var val = this.indexPopupWindowHrefUrl;
				if (this.indexPopupWindowHrefUrl.indexOf('http') != -1) {
					// #ifdef APP-PLUS || APP-PLUS-NVUE || MP
					this.$u.route('/pages/webview/webview', {
						src: val
					});
					// #endif
				} else {
					// #ifdef APP-PLUS || APP-PLUS-NVUE || MP
					if (val == '/pages/index/default/default' || val == '/pages/category/index/index' || val ==
						'/pages/index/cart/cart' || val == '/pages/index/member/member') {
						this.$u.route({
							type: 'switchTab',
							url: val
						});
						return;
					} else if (val.indexOf('/pages/coupon/coupon?id=') > -1) {
						var id = val.replace('/pages/coupon/coupon?id=', "");
						this.receiveCoupon(id)
					} else {
						this.$u.route(val);
						return;
					}
					// #endif
				}
			},
			closeTap() {
				this.modalShow = false;
			},
			getCaeds(){
				this.$u.api.CardItemList({page:1, limit: 3}).then(res => {
					if(res.code === 0){
						this.cardData.parameters.list = res.data
					}else {
						this.$u.toast("获取卡项数据失败");
					}
				});
			},
			getProjects(){
				this.$u.api.projectList({page:1, limit: 4}).then(res => {
					this.projectData.parameters.list = res.data
				});
			},
			// 首页初始化获取数据
			initData() {
				uni.showLoading({
					title: '加载Home数据'
				});
				//获取首页配置
				this.$u.api.getPageConfig({
					code: this.pageCode
				}).then(res => {
					if (res.status === true) {
						this.pageData = res.data.items;
						// const storeData = {widgetCode:'store', id:'7000', storeName:this.userStore.name}
						// this.pageData.unshift(storeData)
						this.pageData.splice(3, 0 , this.cardData)
						this.pageData.splice(5, 0 , this.projectData)
					}else {
						this.$u.toast(res.msg);
					}
					setTimeout(function() {
						uni.hideLoading();
					}, 1000);
				});

				var _this = this;
				if (this.$db.get('userToken')) {
					this.$u.api.userInfo().then(res => {
						if (res.status) {
							_this.userInfo = res.data;
							_this.hasLogin = true;
							// #ifdef MP-WEIXIN
							//微信小程序打开客服时，传递用户信息
							var kefupara = {};
							kefupara.nickName = res.data.nickName;
							kefupara.tel = res.data.mobile;
							_this.kefupara = JSON.stringify(kefupara);
							//console.log(_this.kefupara);
							// #endif
						}
					});
				}
				this.getShareUrl();
			},
			//在线客服
			showChat() {
				// #ifdef APP-PLUS || APP-PLUS-NVUE
				this.$u.route('/pages/member/customerService/index');
				// #endif
			},
			//获取分享URL
			getShareUrl() {
				let data = {
					client: this.$globalConstVars.shareClient.wxMiNiProgram,
					url: this.$globalConstVars.shareUrl,
					type: this.$globalConstVars.shareModel.url,
					page: this.$globalConstVars.shareType.index,
				};
				let userToken = this.$db.get('userToken');
				if (userToken && userToken != '') {
					data.token = userToken
				}
				this.$u.api.share(data).then(res => {
					this.shareUrl = res.data
				});
			},
		},
		onPullDownRefresh() {
			this.initData();
			uni.stopPullDownRefresh();
		},
		//分享
		onShareAppMessage(res) {
			return {
				title: this.$store.state.config.shareTitle,
				imageUrl: this.$store.state.config.shareImage,
				path: this.shareUrl
			}
		},
		onShareTimeline(res) {
			return {
				title: this.$store.state.config.shareTitle,
				imageUrl: this.$store.state.config.shareImage,
				path: this.shareUrl
			}
		},
		onPageScroll: function(e) {
			this.isScorll = e.scrollTop > 100 ? true : false;
			this.scrollTop = e.scrollTop;
			if (e.scrollTop <= 100) {
				this.opacity = e.scrollTop / 100;
			} else if (this.scrollTop > 100) {
				this.opacity = 1;
			}
		},
	};
</script>

<style lang="scss">

</style>
